package com.day18.FileToArray;

import com.day18.Student.Student;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class FileArrayListDemo {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("myJava\\FileArrayListDemo.java"));
        ArrayList<Student> array = new ArrayList<>();
        String line;

        while ((line = br.readLine()) != null) {
            String[] strArray = line.split(" ");
            if (strArray.length >= 4) { // 防止数组越界
                Student s = new Student();
                s.setSid(strArray[0]);
                s.setName(strArray[1]);
                s.setAge(Integer.parseInt(strArray[2]));
                s.setAddress(strArray[3]);
                array.add(s);
            } else {
                System.out.println("Invalid data format: " + line);
            }
        }

        br.close();

        for (Student s : array) {
            System.out.println(s.getSid() + "," + s.getName() + "," + s.getAge() + "," + s.getAddress());
        }
    }
}

